package com.choosefine.it.basedata.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.util.UriEncoder;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.TeamTypeClass;
import com.choosefine.it.basedata.model.vo.TeamTypeClassFindVO;
import com.choosefine.it.basedata.service.TeamTypeClassService;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.common.utils.DateUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 班组父类相关控制层
 * 
 * @author guwenwei 2016/11/20
 */
@RestController
@Api(value = "班组父类别相关", description = "顾文伟")
@RequestMapping("/basedata/teamtypeclass")
public class TeamTypeClassController extends BaseController {
	Logger logger = LoggerFactory.getLogger(TeamTypeClassController.class);

	@Autowired
	TeamTypeClassService teamtypeClassService;

	/**
	 * 添加班组父类别
	 * 
	 * @param teamtype
	 * @param response
	 * @return
	 * @throws BusinessException 
	 * @throws Exception
	 */
	@ApiOperation(value = "存储班组父类别", notes = "传入班组父类别对象，存储相关班组父类别信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.POST)
	public String save(@RequestBody TeamTypeClass teamtype) throws BusinessException {
		String result;
		if (teamtype != null) {
			teamtype.setCtime(DateUtils.getNowTime());
			int num = teamtypeClassService.insert(teamtype);
			if (num > 0) {
				result = responseSuccess(num,ResultCode.SUCCESS_CREATED,ResultCode.MSG_CREATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_CREATE_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 更新班组父类型数据
	 * 
	 * @param teamtype
	 *            父类型名
	 * @return
	 * @throws BusinessException 
	 */
	@ApiOperation(value = "更改班组父类型", notes = "传入班组父类别对象，更改相关班组父类别信息")
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.PUT)
	public String update(@RequestBody TeamTypeClass teamtype) throws BusinessException {
		String result = null;
		if (teamtype != null) {
			teamtype.setUtime(DateUtils.getNowTime());
			int num = teamtypeClassService.update(teamtype);
			if (num > 0) {
				result = responseSuccess(num,ResultCode.SUCCESS_CREATED,ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_UPDATE_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 根据ID获取班组父类型名
	 * 
	 * @param id
	 *            班组父类型ID
	 * @return
	 */
	@ApiOperation(value = "根据ID取班组父类别", notes = "传入班组父类别ID，根据ID获取班组父类别信息")
	@ApiImplicitParam(name = "id", value = "班组父类别ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String getTeamTypeById(@PathVariable Integer id) {
		String result = null;
		if (id >= 0) {
			TeamTypeClass teamtype = teamtypeClassService.findByPrimaryKey(id);
			if (teamtype != null) {
				result = responseSuccess(teamtype);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 根据ID删除班组父类型
	 * 
	 * @param id
	 *            班组父类型ID
	 * @return
	 */
	@ApiOperation(value = "根据ID删除班组父类别", notes = "传入班组父类别ID，根据ID删除班组父类别信息")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable Integer id) {
		String result = null;
		if (id != null) {
			int num = teamtypeClassService.deleteByPrimaryKey(id);
			if (num > 0) {
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;

	}

	@ApiOperation(value = "获取所有班组父类", notes = "")
	@RequestMapping(value = "/teamtypeclasslist", method = RequestMethod.GET)
	public String getTeamTypeClassList() throws BusinessException {
		List<TeamTypeClass> teamtypeclass = teamtypeClassService.findAllByPage();
		return responseArraySuccess(teamtypeclass);
	}
	
	@ApiOperation(value = "根据名称获取班组类别", notes = "")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "pageNum", value = "起始页", dataType = "Integer", required = true, paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "每页行", dataType = "Integer", required = true, paramType = "query") })
	@ApiImplicitParam(name = "typeclassname", value = "班组类型", dataType = "String", required = true, paramType = "path")
	@RequestMapping(value = "/teamtypeclasswithname/{typeclassname}", method = RequestMethod.GET)
	public String getTeamTypeWithName(@PathVariable String typeclassname, Integer pageSize, Integer pageNum) {
		String result = null;
		PageInfo<TeamTypeClass> teamtypeclass = teamtypeClassService.selectTeamTypeClassByName(UriEncoder.decode(typeclassname), pageNum, pageSize);
		if (teamtypeclass != null) {
			result = responseArraySuccess(teamtypeclass);
		} else {
			result = responseFail(ResultCode.MSG_DATA_EMPTY);
		}
		return result;
	}
	@ApiOperation(value = "班组类别排序")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "fromId", value = "移动项", dataType = "Integer", required = true, paramType = "path"),
		@ApiImplicitParam(name = "toId", value = "移动到的位置", dataType = "Integer", required = true, paramType = "path"),
		@ApiImplicitParam(name = "order", value = "移动标识（1上移-1下移）", dataType = "Integer", required = true, paramType = "path")
	})
	@RequestMapping(value = "/teamTypeClassOrder/{fromId}/{toId}/{order}", method = RequestMethod.PUT)
	public String teamTypeClassOrder(@PathVariable Integer fromId, @PathVariable Integer toId, @PathVariable Integer order){
		String result = null;
		int num = 0;
		if(order>0){
			num += teamtypeClassService.teamTypeClassOrderUp(fromId, toId);
		}else{
			num += teamtypeClassService.teamTypeClassOrderDown(fromId, toId);
		}
		if(num>0){
			result = responseSuccess(num);
		}else{
			result = responseFail();
		}
		return result;
	}
	
	/**
	 * @Description：查询所有一级班组类目(陈健)
	 * @Author：chenjian
	 * @Create Date：2017年2月18日
	 * @param：
	 * @return：
	 */
	@ApiOperation(value = "查询所有一级班组类目(陈健)")
	@RequestMapping(value = "/teamTypeFirstList", method = RequestMethod.GET)
	public String getTeamTypeFirstList() {
		List<TeamTypeClassFindVO> teamTypeClassFindVOList = teamtypeClassService.getTeamTypeFirstList();
		if(teamTypeClassFindVOList!=null&&teamTypeClassFindVOList.size()>0){
			return responseArraySuccess(teamTypeClassFindVOList);
		}else{
			return responseFail(ResultCode.MSG_DATA_ISEMPTY);
		}
	}
}
